//
//  UIViewController+Extension.swift
//  RE_BasePrivateProject
//
//  Created by mac on 2018/10/08.
//  Copyright © 2018年 Regina. All rights reserved.
//

import Foundation
import UIKit

extension UIViewController{
    
    public func disablesAdjustScrollViewInsets(_ scrollView: UIScrollView) {
        self.automaticallyAdjustsScrollViewInsets = false
        if #available(iOS 11.0, *) {
            scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            self.extendedLayoutIncludesOpaqueBars = true
            automaticallyAdjustsScrollViewInsets = false
        }
    }
    
//    public convenience init<T:REViewModelType>(_ viewModel:T) {
//        self.init()
//    }
    
    @objc public dynamic func REViewDidLoad() {
        self.edgesForExtendedLayout = .bottom

        let image = UIImage(named: "RE_base_back")?.withRenderingMode(.alwaysOriginal)
        
        self.navigationItem.backBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: nil, action: nil)
//        if #available(iOS 11.0,*) {
//            self.automaticallyAdjustsScrollViewInsets = true
//        } else {
//            self.automaticallyAdjustsScrollViewInsets = false
//        }
//        self.extendedLayoutIncludesOpaqueBars = true
//        self.navigationController?.navigationBar.isTranslucent = true
    }
    
    public func REViewWillAppear() {
        self.navigationController?.showNavigationBottomLine()

//        if self.viewModel.prefersNavigationBarHidden != nil {
//        }else {
//            self.navigationController?.showNavigationBottomLine()
//        }
        
    }
    
    public func createBackNaviItem() {
        
        let image = UIImage(named: "RE_base_back")?.withRenderingMode(.alwaysOriginal)
        self.navigationItem.backBarButtonItem = UIBarButtonItem(image: image, style: .plain, target: nil, action: nil)
    }
    
}

extension UIViewController {
    
    ///按照索引返回到某个控制器，从当前控制器开始向前+1
    public func popToVC(previous: Int, animated: Bool = true) {
        if (self.navigationController?.viewControllers.count)! >= previous {
            let vc = self.navigationController?.viewControllers[(self.navigationController?.viewControllers.count)! - previous]
            self.navigationController?.popToViewController(vc!, animated: animated)
        }
    }
    
    ///返回
    public func pop(_ animated: Bool = true) {
        self.navigationController?.popViewController(animated: animated)
    }
    
    ///返回
    public func popToRoot(_ animated: Bool = true) {
        self.navigationController?.popToRootViewController(animated: animated)
    }
    
    ///返回
    public func popToVC(_ vc: UIViewController, animated: Bool = true) {
        self.navigationController?.popToViewController(vc, animated: animated)
    }

    ///跳转
    public func push(_ vc: UIViewController, animated: Bool = true) {
        
        if self.tabBarController != nil && vc.hidesBottomBarWhenPushed == false {
            vc.hidesBottomBarWhenPushed = true
        }
        self.navigationController?.pushViewController(vc, animated: animated)
    }
}

//MARK: - 刷新
extension UIViewController {

    public func RERefreshHeader(_ refreshingBlock: @escaping MJRefreshComponentRefreshingBlock) -> MJRefreshNormalHeader {

        let header = MJRefreshNormalHeader.init(refreshingBlock: refreshingBlock)!
        header.isAutomaticallyChangeAlpha = true
        header.lastUpdatedTimeLabel.isHidden = true
        header.setTitle(globalLanguage("Global_RefreshIdleTitle"), for: MJRefreshState.idle)
        header.setTitle(globalLanguage("Global_RefreshPulling"), for: MJRefreshState.pulling)
        header.setTitle(globalLanguage("Global_Refreshing"), for: MJRefreshState.refreshing)
        return header
    }

    public func RERefrshFooter(_ refreshingBlock: @escaping MJRefreshComponentRefreshingBlock) -> MJRefreshAutoNormalFooter {
        let footer = MJRefreshAutoNormalFooter.init(refreshingBlock: refreshingBlock)!
        footer.isHidden = true
        footer.setTitle(globalLanguage("Global_LoadMoreIdleTitle"), for: MJRefreshState.idle)
        footer.setTitle(globalLanguage("Global_RefreshPulling"), for: MJRefreshState.pulling)
        footer.setTitle(globalLanguage("Global_LoadMoreing"), for: MJRefreshState.refreshing)
        footer.setTitle(globalLanguage("Global_LoadMoreNoMoreData"), for: MJRefreshState.noMoreData)
        return footer
    }

    public func RERefreshHeader() -> MJRefreshNormalHeader {

        let header = MJRefreshNormalHeader()
        header.isAutomaticallyChangeAlpha = true
        header.lastUpdatedTimeLabel.isHidden = true
        return header
    }

    public func RERefrshFooter() -> MJRefreshAutoNormalFooter {
        let footer = MJRefreshAutoNormalFooter()
        footer.isHidden = true
        return footer
    }
}
//
//
//extension UIViewController {
//
//    public func re_showTast(_ message: String?) {
//        self.view.re_showTast(message)
//    }
//}



